package com.z2l2y.start;

import com.z2l2y.start.banner.Z2l2yBanner;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author z2l2y
 */
@SpringBootApplication
public class BookkeepingProvideApplication {

    public static void main(String[] args) throws UnknownHostException {
        SpringApplication application = new SpringApplication(BookkeepingProvideApplication.class);

        // 设置banner
        application.setBannerMode(Banner.Mode.CONSOLE);
        Banner banner = new Z2l2yBanner();
        application.setBanner(banner);

        // 启动容器
        ConfigurableApplicationContext context = application.run(args);

        // 启动输出
        printStarted(context);

    }

    public static void printStarted(ConfigurableApplicationContext context) throws UnknownHostException {
        Environment env = context.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        port = port == null ? "8080" : port;
        String path = env.getProperty("server.servlet.context-path");
        path = path == null ? "" : path;
        System.out.format(
                """
                \33[36;3m走不尽的山峦与起伏, 献给披星戴月的三千里路\33[3m
                -----------------------------------------------------------
                - 本地访问地址: http://localhost:%s/%s                     -
                - 外部访问地址: http://%s:%s/%s                   -
                -----------------------------------------------------------
                """,
                port, path,
                ip, port, path
        );
    }
}
